package upicgame.article.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import upicgame.article.service.ArticleService;
import upicgame.entity.Article;
import upicgame.entity.User;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @program: upicgame
 * @description: article控制器
 * @author: lhq
 * @create: 2020-01-03 10:48
 */
@Controller
@CrossOrigin
public class ArticleController {
	@Resource
	ArticleService articleService;

	@RequestMapping(value = "showArticle",produces = "application/json; charset=UTF-8")
	@ResponseBody
	public Article showArticle(Integer id){
		ModelAndView modelAndView = new ModelAndView();
		Article article = articleService.selectArticle(id);
		//modelAndView.addObject("article" ,article);
		//modelAndView.setViewName("md.jsp");
		return article;
	}
	@RequestMapping(value = "addArticle")
	public ModelAndView addArticle(Article article, HttpServletRequest request){
		ModelAndView modelAndView = new ModelAndView();
		System.out.println(article);
		System.out.println("添加文章");
		HttpSession httpSession = request.getSession();
		User user = (User) httpSession.getAttribute("user");
		if (user != null){
			article.setUser(user);
			boolean addArticle = articleService.addArticle(article);
			modelAndView.setViewName("article.html");
		}else {
			modelAndView.setViewName("index.jsp");
		}
		return modelAndView;
	}
	@RequestMapping(value = "delArticle")
	public String delArticle(Integer id){
		if(id != null){
			boolean delArticle = articleService.delArticle(id);
			return "articleList.html";
		}else {
			return "articleList.html";
		}
	}
	@RequestMapping(value = "updateArticle")
	public ModelAndView updateArticle(Article article){
		ModelAndView modelAndView = new ModelAndView();
		boolean updateArticle = articleService.updateArticle(article);
		modelAndView.addObject("article",article);
		modelAndView.setViewName("edit.jsp");
		return modelAndView;
	}
	@RequestMapping(value = "articleList" ,produces = "application/json; charset=UTF-8")
	@ResponseBody
	public String articleList(Integer pageNum, Integer size){
		System.out.println("pageNum:"+pageNum+"____"+"size:"+size);
		pageNum = (pageNum == null) ? 1 : pageNum;
		size = (size == null )? 1: size;
		List<Article> articles = articleService.articleList(pageNum,size);
		String json = JSON.toJSONString(articles);
		System.out.println(json);
		return json;
	}
	@RequestMapping(value = "getCount")
	@ResponseBody
	public Long getLong(){
		return articleService.getCount();
	}
	@RequestMapping(value = "getTitle",produces = "application/json; charset=UTF-8")
	@ResponseBody
	public List getTitle(){
		return articleService.getTitle();
	}
	@RequestMapping(value = "saveUpdateId")
	public String saveUpdateId(Integer id,HttpServletRequest request){
		request.getSession().setAttribute("id",id);
		return "redirect:edit.html";
	}
	@RequestMapping(value = "getUpdateId")
	@ResponseBody
	public Integer getUpdateId(HttpServletRequest request){
		Integer id =(Integer) request.getSession().getAttribute("id");
		System.out.println(id);
		return id;
	}
}
